using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using XmlObjects;

namespace XmlObjects.Parsers
{
	public class ArrayParser<T> : Parser<T[]>
	{
		private readonly ILocalNodeParser<T> baseParser;

		public ArrayParser(PropertyAttribute attribute)
		{
			this.SelectString = attribute.SelectString;
			baseParser = Parser.GetLocalNodeParser<T>();
		}

		public override T[] GetValue(XmlNode node)
		{
			XmlNodeList nodeList = node.SelectNodes(SelectString);
			if (nodeList==null || nodeList.Count==0)
			{
				return new T[0];
			}
			else
			{
				T[] returnVal = new T[nodeList.Count];

				for (int i = 0; i < returnVal.Length; i++ )
				{
					returnVal[i] = baseParser.GetValueLocal(nodeList[i]);
				}
				return returnVal;
			}
		}

		public override T[] GetValueLocal(XmlNode node)
		{
			throw new NotImplementedException();
		}
	}
}
